package com.diudiutechnology.www.yadiudiu.yadiudiu.mine.MyActivity;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.diudiutechnology.www.yadiudiu.util.http.HttpClient;
import com.diudiutechnology.www.yadiudiu.util.http.JsonTools;
import com.diudiutechnology.www.yadiudiu.util.utils.CountDownButtonHelper;
import com.diudiutechnology.www.yadiudiu.util.utils.MyProgressDialog;
import com.diudiutechnology.www.yadiudiu.util.utils.Sundry;
import com.diudiutechnology.www.yadiudiu.util.utils.ToastUtils;
import com.diudiutechnology.www.yadiudiu.yadiudiu.main.MyActivity.BaseActivity;
import com.diudiutechnology.www.yadiudiu.yadiudiu.model.RegistrationModel;
import com.diudiutechnology.www.yadiudiu.R;

import org.xutils.common.Callback;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/8/16.
 * 重置密码
 */

public class ToResetYourPasswordActivity extends BaseActivity implements View.OnClickListener ,Callback.CommonCallback<String>{

    @Bind(R.id.retur)
    LinearLayout retur;
    @Bind(R.id.phonenumber)//手机号
            EditText phonenumber;

    @Bind(R.id.admin_yzm)//验证码
            EditText admin_yzm;

    @Bind(R.id.admin_pow)//密码
            EditText admin_pow;

    @Bind(R.id.confirm_pow)//确认密码
            EditText confirm_pow;

    @Bind(R.id.admin_btn_yzm)//获取验证
            Button admin_btn_yzm;

    @Bind(R.id.button)//注册
            Button button;


    Context context;
    String Judge_interface="0";//判断哪个接口   0、短信  1、重置
    private MyProgressDialog mDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_to_reset_your_password);
        ButterKnife.bind(this);
        context=this;
        initView();
    }

    private void initView() {
        retur.setOnClickListener(this);
        admin_btn_yzm.setOnClickListener(this);
        button.setOnClickListener(this);
        initData();
    }

    private void initData() {

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.retur:
                finish();
                break;
            case R.id.admin_btn_yzm://短信
                if (TextUtils.isEmpty(phonenumber.getText().toString().trim())){
                    ToastUtils.show(context, getResources().getString(R.string.please_phone_number));
                    return;
                }
                Judge_interface="0";
                setPhonenumber();
                break;
            case R.id.button://注册
                if (TextUtils.isEmpty(phonenumber.getText().toString().trim())){//手机号
                    ToastUtils.show(context, getResources().getString(R.string.please_phone_number));
                    return;
                }
                if (!Sundry.isMobileNO(phonenumber.getText().toString().trim())){//验证手机号
                    ToastUtils.show(context, getResources().getString(R.string.please_correctness_phone_number));
                    return;
                }

                if (TextUtils.isEmpty(admin_yzm.getText().toString().trim())){//验证码
                    ToastUtils.show(context, getResources().getString(R.string.please_verification_code));
                    return;
                }
                if (TextUtils.isEmpty(admin_pow.getText().toString().trim())){//密码
                    ToastUtils.show(context, getResources().getString(R.string.please_login_password));
                    return;
                }
                if (admin_pow.getText().toString().trim().length()<6){//密码大于6位
                    ToastUtils.show(context, getResources().getString(R.string.please_correctness_login_password));
                    return;
                }
                if (TextUtils.isEmpty(confirm_pow.getText().toString().trim())){//确认密码
                    ToastUtils.show(context, getResources().getString(R.string.please_again_login_password));
                    return;
                }
                if (!admin_pow.getText().toString().trim().equals(confirm_pow.getText().toString().trim())){//两次密码对比
                    ToastUtils.show(context, getResources().getString(R.string.two_passwords_are_different));
                    return;
                }
                Judge_interface="1";
                setPhonenumber();
                break;

        }
    }
    //提交短信 和找回密码
    public void setPhonenumber(){
        if (Sundry.isOpenNetwork(context)){
            mDialog =new MyProgressDialog(context);
        if (Judge_interface.equals("0")){
            HttpClient.note(phonenumber.getText().toString().trim(), "2", this);
        }else {
         HttpClient.Retrieve_password(phonenumber.getText().toString().trim(), admin_pow.getText().toString().trim(),admin_yzm.getText().toString().trim(), this);
        }
        }else {
            ToastUtils.Toastshow(context, getResources().getString(R.string.network));
        }
    }
    @Override
    public void onSuccess(String result) {
        if (mDialog!=null){
            mDialog.dismiss();
        }
        System.out.println("stringmd5result"+result);
        List<RegistrationModel> getnote = JsonTools.getnote(result);
        if (getnote!=null||getnote.size()>0){
            String status = getnote.get(0).getStatus();
            String getMsg = getnote.get(0).getMsg();
            if (status.equals("200")){
                if (Judge_interface.equals("0")){
                    // 验证码
                    CountDownButtonHelper helper = new CountDownButtonHelper(admin_btn_yzm, 120, 1);
                    helper.start();
                    ToastUtils.show(context, getMsg);
                }else {
                    ToastUtils.show(context, getMsg);
                }

            }else {
                ToastUtils.show(context, getMsg);
            }
        }
    }

    @Override
    public void onError(Throwable ex, boolean isOnCallback) {
        if (mDialog!=null){
            mDialog.dismiss();
        }
        ToastUtils.Toastshow(context, getResources().getString(R.string.server));
    }

    @Override
    public void onCancelled(CancelledException cex) {
        if (mDialog!=null){
            mDialog.dismiss();
        }
        ToastUtils.Toastshow(context, getResources().getString(R.string.server));
    }

    @Override
    public void onFinished() {

    }
}
